package com.pbdevj.ssm.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pbdevj.ssm.bean.Employee;
import com.pbdevj.ssm.service.EmployeeService;
import com.pbdevj.ssm.util.GenerateJsonMessageWithPageHelper;

//@RestController
@Controller
@RequestMapping("/employee")
public class EmployeeController
{
	@Autowired
	@Qualifier("employeeServiceImpl")
	private EmployeeService employeeService;

//	@RequestMapping(value="/employees", method=RequestMethod.GET)
	public String getEmployees(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, Model model)
	{
		PageHelper.startPage(pageNum, 5);

		List<Employee> employees = this.employeeService.getAll();

		PageInfo<Employee> pageInfo = new PageInfo<Employee>(employees, 5);

		model.addAttribute("pageInfo", pageInfo);

		return "list";
	}

	/**
	 * 查询带部门的雇员信息
	 * @param pageNum
	 * @return
	 */
	@RequestMapping(value="/employees", method=RequestMethod.GET)
	@ResponseBody
	public GenerateJsonMessageWithPageHelper getEmployeesWithDept(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum)
	{
		PageHelper.startPage(pageNum, 5);

		List<Employee> employees = this.employeeService.getAll();

		PageInfo<Employee> pageInfo = new PageInfo<Employee>(employees, 5);
		
		GenerateJsonMessageWithPageHelper jsonMessage = GenerateJsonMessageWithPageHelper.success().add("pageInfo", pageInfo);

		return jsonMessage;
	}
	
	/**
	 * 保存雇员信息
	 * @param employee
	 * @return
	 */
	@RequestMapping(value = "/employee", method=RequestMethod.POST)
	@ResponseBody
	public GenerateJsonMessageWithPageHelper saveEmployee(@Valid Employee employee, BindingResult br)
	{
		if(br.hasErrors())
		{
			//如果校验失败，在模态框中显示校验失败信息
			List<FieldError> errors = br.getFieldErrors();
			
			Map<String, Object> errorMessages = new HashMap<>();
			
			for(FieldError fieldError : errors)
			{
				errorMessages.put(fieldError.getField(), fieldError.getDefaultMessage());
			}
			
			return GenerateJsonMessageWithPageHelper.fail().add("errors", errorMessages);
		}
		else{
			this.employeeService.saveEmployee(employee);
			
			return GenerateJsonMessageWithPageHelper.success();
		}
		
		
	}
	
	/**
	 * 检查雇员名是否可用
	 * @param empName
	 * @return
	 */
	@RequestMapping(value = "/checkedEmpName", method=RequestMethod.GET)
	@ResponseBody
	public GenerateJsonMessageWithPageHelper checkedEmpName(@RequestParam("empName") String empName)
	{
		String strReg = "(^[a-zA-Z0-9_-]{5,16}$)|(^[\\u2E80-\\u9FFF]{2,5}$)";
		
		if(!strReg.matches(empName))
		{
			return GenerateJsonMessageWithPageHelper.fail().add("validate_fail", "雇员名字必须是6-12位字母和数字或者是2-6个中文字符");
		}
		
		
		boolean flag = this.employeeService.checkedEmpName(empName);
		
		if(flag)
		{
			return GenerateJsonMessageWithPageHelper.success();
		}
		else
		{
			return GenerateJsonMessageWithPageHelper.fail().add("validate_fail", "用户名不可用！");
		}
	}
	
	/**
	 * 根据id查询员工
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/employee/{empId}", method=RequestMethod.GET)
	@ResponseBody
	public GenerateJsonMessageWithPageHelper getEmployeeById(@PathVariable("empId") Integer id)
	{
		Employee employee = this.employeeService.getEmployeeById(id);
		
		return GenerateJsonMessageWithPageHelper.success().add("employee", employee);
	}
	
	
	/**
	 * 员工更新方法
	 * @param employee
	 * @return
	 */
	@RequestMapping(value = "/employee/{empId}", method=RequestMethod.PUT)
	@ResponseBody
	public GenerateJsonMessageWithPageHelper updateEmployee(Employee employee)
	{
		
		this.employeeService.updateEmployee(employee);
		
		return GenerateJsonMessageWithPageHelper.success();
	}
	
	/**
	 * 批量删除和单个删除方法
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/employee/{ids}", method=RequestMethod.DELETE)
	@ResponseBody
	public GenerateJsonMessageWithPageHelper deleteEmployee(@PathVariable("ids")String ids)
	{
		List<Integer> deleteIds = new ArrayList<>();
		
		if(ids.contains("-"))
		{
			String[] idArr = ids.split("-");
			
			for (String strId : idArr)
			{
				Integer id = Integer.parseInt(strId);
				
				deleteIds.add(id);
			}
			
			this.employeeService.deleteBatchedEmployee(deleteIds);
			
		}
		else
		{
			Integer id = Integer.parseInt(ids);
			
			this.employeeService.deletEmployeeById(id);

		}
		
		
		return GenerateJsonMessageWithPageHelper.success();
	}

}
